package com.simplemall.micro.serv.page.api;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.simplemall.micro.serv.common.bean.RestAPIResult;
import com.simplemall.micro.serv.common.bean.telbook.TelBook;
import com.simplemall.micro.serv.common.bean.telbook.TelBookInfo;
import com.simplemall.micro.serv.common.constant.SystemConstants;
import com.simplemall.micro.serv.page.client.ProductFeignClient;
import com.simplemall.micro.serv.page.client.TelBookFeignClient;

@Api(value = "电话本服务", tags = "电话本服务")
@RestController
@RequestMapping("/telBook")
public class APITelBookController {

	private Logger logger = LoggerFactory.getLogger(APITelBookController.class);
	
	@Autowired
	TelBookFeignClient feignClient;

	@ApiOperation(value = "获取电话组")
	@RequestMapping(value = "telBook/{userid}", method = RequestMethod.POST)
	// 必须采用pathvariable的方式引入参数
	public RestAPIResult<TelBook> showTelBookByUserId(@PathVariable("userid") String userid) {
		RestAPIResult<TelBook> restAPIResult = new RestAPIResult<>();
		logger.info("begin invoke telBook select service");
		List<TelBook> info = feignClient.getTelBookById(userid);
		if(info.isEmpty()){
			restAPIResult.error(new TelBook());
		}else{
			restAPIResult.setRespData(info.get(0));
		}
		return restAPIResult;
	}
	
	@ApiOperation(value = "创建电话组")
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public RestAPIResult<String> createTelBook(@RequestParam("userid") String userid,
			@RequestParam("remark") String remark, @RequestParam("bookName") String bookName){
		RestAPIResult<String> restAPIResult = new RestAPIResult<>();
		logger.info("begin invoke telBook create service");
		String resultStr = feignClient.createTelBook(bookName, remark, userid);
		if(SystemConstants.TEL_BOOK.UPPERLIMIT.equals(resultStr)){
			restAPIResult.error();
		}
		restAPIResult.success(resultStr);
		return restAPIResult;
	}
	
	@ApiOperation(value = "添加电话组列表")
	@RequestMapping(value = "insert", method = RequestMethod.POST)
	public RestAPIResult<String> insertTelBookInfo(@RequestParam("telBookStr") String telBookStr ){
		RestAPIResult<String> restAPIResult = new RestAPIResult<>();
		logger.info("begin invoke telBookInfo create service");
		String resultStr = feignClient.insertTelBookInfo(telBookStr);
		if(SystemConstants.TEL_BOOK.UPPERLIMIT.equals(resultStr)){
			restAPIResult.error();
		}
		restAPIResult.success(resultStr);
		return restAPIResult;
	}
	
	@ApiOperation(value = "查看电话组列表")
	@RequestMapping(value = "telBookInfo/{telBookId}", method = RequestMethod.POST)
	public RestAPIResult<List<TelBookInfo>> showTelBookInfoList(@PathVariable("telBookId") String telBookId ){
		RestAPIResult<List<TelBookInfo>> restAPIResult = new RestAPIResult<>();
		logger.info("begin invoke telBook show service");
		List<TelBookInfo> result = feignClient.telBookInfo(telBookId);
		restAPIResult.success(result);
		return restAPIResult;
	}

	@ApiOperation(value = "查看当前电话组列表中的信息")
	@RequestMapping(value = "telBookInfo/{telBookId}/{fuserid}", method = RequestMethod.POST)
	public RestAPIResult<TelBookInfo> showTelBookInfoByUserid(@PathVariable("telBookId") String telBookId 
			,@PathVariable("fuserid") String fuserid){
		RestAPIResult<TelBookInfo> restAPIResult = new RestAPIResult<>();
		logger.info("begin invoke telBookInfo getBookInfoByUserid service");
		TelBookInfo result = feignClient.getTelBookInfoByFuserid(telBookId, fuserid);
		restAPIResult.success(result);
		return restAPIResult;
	}

	@ApiOperation(value = "修改电话组详细信息")
	@RequestMapping(value = "updateTelBookInfo", method = RequestMethod.POST)
	public RestAPIResult<String> updateTelBookInfo(@RequestParam("telBookInfoStr") String telBookInfoStr ){
		RestAPIResult<String> restAPIResult = new RestAPIResult<>();
		logger.info("begin invoke telBookInfo update service");
		String resultStr = feignClient.updateTelBookInfo(telBookInfoStr);
		restAPIResult.success(resultStr);
		return restAPIResult;
	}
	
	@ApiOperation(value = "删除电话组详细信息")
	@RequestMapping(value = "deleteTelBookInfo/{telBookInfoId}", method = RequestMethod.POST)
	public RestAPIResult<String> deleteTelBookInfo(@RequestParam("telBookInfoId") String telBookInfoId ){
		RestAPIResult<String> restAPIResult = new RestAPIResult<>();
		logger.info("begin invoke telBookInfo delete service");
		String resultStr = feignClient.deleteTelBookInfo(telBookInfoId);
		restAPIResult.success(resultStr);
		return restAPIResult;
	}
	/**
	 * 校验用户是否登陆
	 * 
	 * @param request
	 * @return
	 */
	// FIXME 安全验证待完善
	private boolean checkAccountOnLine(String jwt) {
		// if (session.getAttribute(WebSecurityConfig.SESSION_KEY) != null)
		// return true;
		return true;
	}
}
